#include "vcd-window-timed-revealer.h"

G_DEFINE_TYPE (SpicyWindowTimedRevealer, spicy_window_timed_revealer, GTK_TYPE_EVENT_BOX)

#define SPICY_WINDOW_TIMED_REVEALER_GET_PRIVATE(obj) \
    (G_TYPE_INSTANCE_GET_PRIVATE ((obj), SPICY_WINDOW_TYPE_TIMED_REVEALER, SpicyWindowTimedRevealerPrivate))

struct _SpicyWindowTimedRevealerPrivate
{
    gboolean fullscreen;
    guint timeout_id;

    GtkWidget *revealer;
};

static void _set_css(GtkWidget *widget, const gchar *data_css)
{
    GtkCssProvider *provider = gtk_css_provider_new ();
    GtkStyleContext *context = gtk_widget_get_style_context(widget);
    gtk_css_provider_load_from_data(provider, data_css, -1, NULL);
    gtk_style_context_add_provider(context, GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_USER);
    g_object_unref(provider);
}

static gboolean schedule_unreveal_timeout_cb(SpicyWindowTimedRevealer *self)
{
    SpicyWindowTimedRevealerPrivate *priv = self->priv;

    gtk_revealer_set_reveal_child(GTK_REVEALER(priv->revealer), FALSE);
    priv->timeout_id = 0;

    return FALSE;
}

static void spicy_window_timed_revealer_unregister_timeout(SpicyWindowTimedRevealer *self)
{
    SpicyWindowTimedRevealerPrivate *priv = self->priv;

    if (priv->timeout_id) {
        g_source_remove(priv->timeout_id);
        priv->timeout_id = 0;
    }
}

static void spicy_window_timed_revealer_schedule_unreveal_timeout(SpicyWindowTimedRevealer *self,
        guint timeout)
{
    SpicyWindowTimedRevealerPrivate *priv = self->priv;

    if (priv->timeout_id != 0)
        return;

    priv->timeout_id = g_timeout_add(timeout,
                                     (GSourceFunc)schedule_unreveal_timeout_cb,
                                     self);
}

static void spicy_window_timed_revealer_grab_notify(SpicyWindowTimedRevealer *self,
                                        gboolean was_grabbed,
                                        gpointer user_data G_GNUC_UNUSED)
{
    if (was_grabbed)
        spicy_window_timed_revealer_schedule_unreveal_timeout(self, 1000);
}

char* vcd_revealer_get_img_full_path(const char* name){

    char icon_path[1000] = {0};

#ifdef __linux__
    //linux 串流+spice
    sprintf(icon_path,"%s%s","/usr/share/spice-gtk/img/",name);
#else

    //if (!scd || !scd->install_path) return NULL;
    sprintf(icon_path, "C:/Program Files (x86)/verycloud/VeryClient/share/img/%s", name);

#endif
    // 未做内存释放
    return g_canonicalize_filename(icon_path, NULL);
}

GtkWidget* vcd_revealer_new_gtkimage(const char* path,int width,int height){

    GdkPixbuf* gpb;
    GtkWidget* image;

    gpb = gdk_pixbuf_new_from_file(path,0);
    if(!gpb) return 0;

    gpb = gdk_pixbuf_scale_simple(gpb,width,height,GDK_INTERP_HYPER);

    image = gtk_image_new_from_pixbuf(gpb);
    return image;
}

static void cb_button_arrow(GtkWidget *widget,gpointer   user_data){

    SpicyWindowTimedRevealer *self = user_data;

    gboolean b = FALSE;

    b = gtk_revealer_get_child_revealed(GTK_REVEALER(self->priv->revealer));

    gtk_revealer_set_reveal_child(GTK_REVEALER(self->priv->revealer),!b);

    // change arrow image
    GtkWidget* image_down = vcd_revealer_new_gtkimage(vcd_revealer_get_img_full_path("topbar-arrow-down-black.png"),24,24);
    GtkWidget* image_up = vcd_revealer_new_gtkimage(vcd_revealer_get_img_full_path("topbar-arrow-up-black.png"),24,24);

    if(image_up && image_down)
        gtk_button_set_image(GTK_BUTTON(widget),!b ? image_up : image_down);
}

static gboolean cb_event_arrow(GtkWidget* widget,GdkEventButton *event,gpointer user_data){

    SpicyWindowTimedRevealer *self = user_data;

    gboolean b = FALSE;

    b = gtk_revealer_get_child_revealed(GTK_REVEALER(self->priv->revealer));

    gtk_revealer_set_reveal_child(GTK_REVEALER(self->priv->revealer),!b);

    return FALSE;
}

SpicyWindowTimedRevealer *spicy_window_timed_revealer_new(GtkWidget *topbar,TopbarPostion position)
{
    SpicyWindowTimedRevealer *self;
    SpicyWindowTimedRevealerPrivate *priv;
    GtkWidget* vbox;
    GtkWidget* button;

    self = g_object_new(SPICY_WINDOW_TYPE_TIMED_REVEALER, NULL);

    priv = self->priv;

    priv->fullscreen = FALSE;
    priv->timeout_id = 0;

    priv->revealer = gtk_revealer_new();
    vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL,0);
    _set_css(vbox,"box{background-color:rgba(255,0,0,0.0);border-radius: 0px 0px 5px 5px}");

    button = gtk_button_new();
    GtkWidget* image = vcd_revealer_new_gtkimage(vcd_revealer_get_img_full_path("topbar-arrow-down-black.png"),24,24);
    if(image)
        gtk_button_set_image(GTK_BUTTON(button),image);
    gtk_button_set_image_position(GTK_BUTTON(button),GTK_POS_TOP);
    _set_css(button,"button{border:none;border-radius:0px 0px 5px 5px}");
    _set_css(button,"button{background-color:rgba(245,245,245,1.0);opacity:1.0}");
    _set_css(button,"button{margin:0px;padding: 0px}");
    _set_css(button,"button{margin-top:-2px}");

    GtkWidget* align = gtk_alignment_new(0.5,0.5,0.0,0.0);
    gtk_box_pack_start(GTK_BOX(vbox),priv->revealer,FALSE,FALSE,0);
    gtk_box_pack_start(GTK_BOX(vbox),align,FALSE,FALSE,0);
    gtk_container_add(GTK_CONTAINER(align),button);
    gtk_container_add(GTK_CONTAINER(priv->revealer), topbar);
    gtk_container_add(GTK_CONTAINER(self), vbox);

    if(position == POSITION_LEFT){
        gtk_widget_set_halign(GTK_WIDGET(self), GTK_ALIGN_START);
        gtk_widget_set_valign(GTK_WIDGET(self), GTK_ALIGN_CENTER);
        gtk_revealer_set_transition_type(priv->revealer,GTK_REVEALER_TRANSITION_TYPE_SLIDE_RIGHT);
    }else if(position == POSITION_RIGHT){
        gtk_widget_set_halign(GTK_WIDGET(self), GTK_ALIGN_END);
        gtk_widget_set_valign(GTK_WIDGET(self), GTK_ALIGN_CENTER);
        gtk_revealer_set_transition_type(priv->revealer,GTK_REVEALER_TRANSITION_TYPE_SLIDE_UP);
    }else{
        gtk_widget_set_halign(GTK_WIDGET(self), GTK_ALIGN_CENTER);
        gtk_widget_set_valign(GTK_WIDGET(self), GTK_ALIGN_START);
    }

    gtk_widget_show_all(GTK_WIDGET(self));

//    g_signal_connect(self,"grab-notify", G_CALLBACK(spicy_window_timed_revealer_grab_notify), NULL);
    g_signal_connect(button, "clicked",G_CALLBACK(cb_button_arrow),self);
    //g_signal_connect(image, "button-release-event",G_CALLBACK(cb_event_arrow),self);

    return self;
}

void spicy_window_timed_revealer_force_reveal(SpicyWindowTimedRevealer *self,
        gboolean fullscreen)
{
    SpicyWindowTimedRevealerPrivate *priv;

    g_return_if_fail(SPICY_WINDOW_IS_TIMED_REVEALER(self));

    priv = self->priv;

    spicy_window_timed_revealer_unregister_timeout(self);
    priv->fullscreen = fullscreen;
    gtk_revealer_set_reveal_child(GTK_REVEALER(priv->revealer), fullscreen);
    spicy_window_timed_revealer_schedule_unreveal_timeout(self, 2000);
}

static void spicy_window_timed_revealer_dispose(GObject *object)
{
    SpicyWindowTimedRevealer *self = SPICY_WINDOW_TIMED_REVEALER(object);
    SpicyWindowTimedRevealerPrivate *priv = self->priv;

    priv->revealer = NULL;

    if (priv->timeout_id) {
        g_source_remove(priv->timeout_id);
        priv->timeout_id = 0;
    }

    G_OBJECT_CLASS(spicy_window_timed_revealer_parent_class)->dispose(object);
}

static void spicy_window_timed_revealer_init(SpicyWindowTimedRevealer *self)
{
    self->priv = SPICY_WINDOW_TIMED_REVEALER_GET_PRIVATE(self);
}

static void spicy_window_timed_revealer_class_init(SpicyWindowTimedRevealerClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS(klass);

    g_type_class_add_private (klass, sizeof (SpicyWindowTimedRevealerPrivate));

    object_class->dispose = spicy_window_timed_revealer_dispose;
}
